import { Injectable } from '@nestjs/common'
import { ApiService } from './moa.service'
import { Cron } from '@nestjs/schedule'
import { AuthorizeDto, MeetingRoomListDto } from '../dtos'
import { InjectModel } from '@nestjs/mongoose'
import { User, UserDocument } from '../schemas'
import { Model } from 'mongoose'

@Injectable()
export class MeetingService {
  constructor(
    private readonly apiService: ApiService,
    @InjectModel(User.name) private userModel: Model<UserDocument>
  ) {}

  @Cron('20 56 12 5 11 *')
  async dueMeetingRoom() {
    console.log(' ——>', '发起预约')
    // const result = await this.apiService.dueMeetingRoom({
    //   city: '深圳市',
    //   address: '中国储能大厦',
    //   room_id: '15',
    //   room_name: '悉尼·乔治街',
    //   week: '星期二',
    //   meeting_date: '2024-11-05',
    //   start_time: '13:30',
    //   end_time: '14:00',
    //   meeting_name: 'erickyi预定的会议室',
    //   meeting_person: '72187',
    //   meeting_type_id: '1',
    //   meeting_person_name: 'erickyi(易瑞文)'
    // })
    // console.log('result ——>', result)
    return {}
  }

  // 获取城市和分公司列表
  async predetermine() {
    return await this.apiService.getPredetermineList()
  }

  // 根据分公司名称和日期查询会议室
  async getMeetingRoomList(query: MeetingRoomListDto) {
    return await this.apiService.getMeetingRoomList(query)
  }

  // 保存一个人的授权信息（cookie）
  async authorize({ oaTokenId, oaSession, mid }: AuthorizeDto) {
    // 有就更新，没有就插入
    await this.userModel.findOneAndUpdate(
      {
        mid
      },
      {
        oaSession,
        oaTokenId
      },
      { upsert: true }
    )
    return null
  }
}
